﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
//using FOBTV.Web.MainClass;

namespace FOBTV.Web.FOBTV2011.UserControl
{
	public partial class RegistrationControl : System.Web.UI.UserControl
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			txtBirthday.Attributes.Add("readonly", "true");
			if (!IsPostBack)
			{
				BindMemberShip();
			}
		}

		private void BindMemberShip()
		{
			System.Data.OleDb.OleDbConnection Conn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("..\\..\\DB\\FOBTVweb.mdb"));
			string strSql = "select MembershipID,MemberShip from MemberShipType";
            FOBTV.Web.MainClass.PageWrapClass pw = new FOBTV.Web.MainClass.PageWrapClass();
			pw.Conn = Conn;
			pw.FillDropDownList(ddlMemberShip, strSql);
		}

		protected void BtnSubmit_Click(object sender, EventArgs e)
		{
			Submit("insert");
		}

		private void Submit(string type)
		{
			System.Data.OleDb.OleDbConnection Conn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("..\\..\\DB\\FOBTVweb.mdb"));
			FOBTV.Web.MainClass.DBClass DBClassTmp = new FOBTV.Web.MainClass.DBClass();
			ArrayList sql = new ArrayList();
			sql.Clear();
            FOBTV.Web.MainClass.User info = new FOBTV.Web.MainClass.User();

			string varPrefix = info.Prefix = this.rblPrefix.SelectedValue;
			string varFirstName = info.FirstName = txtFirstName.Text.Trim();
			string varLastName = info.LastName = txtLastName.Text.Trim();
			string varBirthday = txtBirthday.Text.Trim();
			string varPassport = info.Passport = txtPassport.Text.Trim();
			string varBusinessTitle = info.BusinessTitle = txtBusinessTitle.Text.Trim();
			string varFullCompanyName = info.FullCompanyName = txtFullCompanyName.Text.Trim();
			string varURL = txtURL.Text.Trim();
			string varAddress = info.Address = txtAddress.Text.Trim();
			string varAddress2 = info.Address2 = txtAddress2.Text.Trim();
			string varAddress3 = info.Address3 = txtAddress3.Text.Trim();
			string varPostalCode = info.PostalCode = txtPostalCode.Text.Trim();
			string varCity = txtCity.Text.Trim();
			string varState = txtState.Text.Trim();

			string varCountry = txtCountry.Text.Trim();
            string varMoblie = txtMoblie.Text.Trim();
			string varPhone = txtPhone.Text.Trim();
			string varFax = txtFax.Text.Trim();
			string varEmail = info.Email = txtEmail.Text.Trim();
            string varRegistrationType = "";
			string varMemberShip = ddlMemberShip.SelectedItem.Text.Trim();
			string varMemberShipID = ddlMemberShip.SelectedItem.Value.Trim();
            string varPassword = FOBTV.Web.MainClass.DEncrypt.Encrypt(txtPassword.Text.Trim());
            info.State = 1;
            info.CreatPDFTime = System.DateTime.Now;
			info.Birthday = Convert.ToDateTime(varBirthday);
			int varCustomerID = 0;
			string strCustomerID = "";
			MainClass.GetUserInfo userInfoClass = new MainClass.GetUserInfo();
			userInfoClass.Conn = Conn;
			userInfoClass.Email = varEmail;
			if ((this.hdModifyID.Value.Length > 0) || (type == "Modify"))
			{
				varCustomerID = Convert.ToInt32(this.hdModifyID.Value.ToString());
				sql.Add("UPDATE  CustomerInfo SET Prefix='" + varPrefix +
					"',FirstName='" + varFirstName +
					"',LastName='" + varLastName +
					"',Birthday='" + varBirthday +
					"',Passport='" + varPassport +
					"',BusinessTitle='" + varBusinessTitle +
					"',FullCompanyName='" + varFullCompanyName +
					"',URL='" + varURL +
					"',Address=" + varAddress +
					 "',Address2='" + varAddress2 +
					"',Address3='" + varAddress3 +
					"',PostalCode='" + varPostalCode +
					"',City='" + varCity +
					"',State='" + varState +
					"',Country='" + varCountry +
					"',Phone='" + varPhone +
					"',Fax=" + varFax +
					 "',Email=" + varEmail +
					"',RegistrationType=" + varRegistrationType +
					"',MemberShip=" + varMemberShip +
					"',PasswordInfo=" + varPassword +
				  " WHERE (CustomerID ='" + this.hdModifyID.Value + "')");
			}
			else
			{
				varCustomerID = DBClassTmp.GetIDInfo("SELECT max(CLng(Right(CustomerID,len(CustomerID)-Instr(1,CustomerID,'V',1)))) FROM CustomerInfo", Conn) + 1;
				strCustomerID = "FOBTV" + varCustomerID.ToString();
				if (!userInfoClass.CheckUserInfoExists())
				{
                    sql.Add("INSERT INTO CustomerInfo(CustomerID,Prefix,FirstName,LastName,Birthday,Passport,BusinessTitle,FullCompanyName,URL,Address,Address2,Address3,PostalCode,City,State,Country,Moblie,Phone,Fax,Email,RegistrationType,MemberShip,PasswordInfo,MemberShipID,CreateTime,StatusID,Status)VALUES ('" +
						strCustomerID + "','" +
						varPrefix + "','" +
						varFirstName + "','" +
						varLastName + "','" +
						varBirthday + "','" +
						varPassport + "','" +
						varBusinessTitle + "','" +
						varFullCompanyName + "','" +
						varURL + "','" +
						varAddress + "','" +
						varAddress2 + "','" +
						varAddress3 + "','" +
						varPostalCode + "','" +
						varCity + "','" +
						varState + "','" +
                        varCountry + "','" +
                        varMoblie + "','" +
						varPhone + "','" +
						varFax + "','" +
						varEmail + "','" +
						varRegistrationType + "','" +
						varMemberShip + "','" +
						varPassword + "','" +
						varMemberShipID + "',#" +
						 System.DateTime.Now.ToString() + "#,'1','CreateUser')");
				}
				else
				{
                    FOBTV.Web.MainClass.WebMessageBox.Show(this.Page, "This user exists!");
					return;
				}

			}
			if (DBClassTmp.ExcuteSQL(sql, Conn))
			{
				if ((this.hdModifyID.Value.Length > 0) || (type == "Modify"))
				{
                    FOBTV.Web.MainClass.WebMessageBox.Show(this.Page, "Modify successfully!!");
				}
				else
				{
                    FOBTV.Web.MainClass.WebMessageBox.Show(this.Page, "Add successfully!!");
				}
				info.CustomerID = strCustomerID;
                FOBTV.Web.MainClass.SessionState.CurrentUser = info;
                if (FOBTV.Web.MainClass.SessionState.CurrentUser.Email != "test@fobtv2011.org")
                {
                    Response.Redirect("RegistarationComplete.aspx");
                }
                else
                {
                    Response.Redirect("RegistrationFee.aspx");
                }

                //switch (FOBTV.Web.MainClass.SessionState.CurrentUser.Email)
                //{
                //    case "admin@fobtv2011.org":
                //        Response.Redirect("CustomerList.aspx");
                //        break;
                //    case "test@fobtv2011.org":
                //        Response.Redirect("RegistrationFee.aspx");
                //        break;
                //    default:
                //        Response.Redirect("RegistarationComplete.aspx");
                //        break;

                //}
			}
			else
			{
                FOBTV.Web.MainClass.WebMessageBox.Show(this.Page, "Operation Failed!!");
			}
		}
	}
}